<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\EduUser;

/**
 * EduUserSearch represents the model behind the search form of `backend\models\EduUser`.
 */
class EduUserSearch extends EduUser
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'sex', 'is_teach', 'update_time', 'parent_id', 'status', 'mall_id', 'mall_is', 'mall_superior', 'mall_top', 'is_red'], 'integer'],
            [['openid', 'nickname', 'city', 'country', 'province', 'phone', 'address', 'info', 'create_time'], 'safe'],
            [['create_time'], 'string']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EduUser::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => ['create_time' => SORT_DESC]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        if ($this->nickname == 1) {
            $query->andFilterWhere([ '<>', 'nickname','游客']);
        } else {
            $query->andFilterWhere(['like', 'nickname', $this->nickname]);
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'sex' => $this->sex,
            'is_teach' => $this->is_teach,
//            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
            'parent_id' => $this->parent_id,
            'status' => $this->status,
            'mall_is' => $this->mall_is,
            'mall_superior' => $this->mall_superior,
            'mall_top' => $this->mall_top,
            'is_red' => $this->is_red,
            'mall_id' => $this->mall_id,
        ]);

        if ($this->create_time != '') {
            $query->andFilterWhere(['between', 'create_time', strtotime($this->create_time), strtotime($this->create_time) + 24 * 60 * 60]);
        }

        $query->andFilterWhere(['like', 'openid', $this->openid])
            ->andFilterWhere(['like', 'nickname', $this->nickname])
            ->andFilterWhere(['like', 'avatar_url', $this->avatar_url])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'country', $this->country])
            ->andFilterWhere(['like', 'province', $this->province])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'info', $this->info]);

        return $dataProvider;
    }
}
